package com.richest.api.controller.cn.merchant.merchant;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.richest.api.service.IEmployeeRoleService;
import com.richest.api.service.IEmployeeService;
import com.richest.bean.EmployeeBean;
import com.richest.bean.EmployeeRoleBean;
import com.richest.util.CallResults;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * API接口 雇员管理功能接口
 * 
 * @author 杨博
 * @version com.richest.api 2.0
 */
@RestController
@RequestMapping("/EmployeeManagerController")
@Api(value = "雇员管理功能接口")
@EnableAutoConfiguration
public class ApiEmployeeManageController {

	@Autowired
	private IEmployeeService employeeService;

	@Autowired
	private IEmployeeRoleService employeeRoleService;

	/**
	 * 雇员登录接口
	 * 
	 * @param logName
	 *            登录名
	 * @param password
	 *            登录密码
	 * @return 雇员信息Bean
	 */
	@RequestMapping(value = "employeeLogin", produces = "application/json;charset=UTF-8")
	@ApiOperation(notes = "雇员登录", value = "雇员登录", httpMethod = "POST")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "logName", value = "雇员登录名", required = true, paramType = "body", dataType = "String"),
			@ApiImplicitParam(name = "password", value = "雇员登录密码", required = true, paramType = "body", dataType = "String") })
	public CallResults employeeLogin(@RequestBody String logName, @RequestBody String password) {
		EmployeeBean bean = employeeService.employeeLogin(logName, password);
		if (bean != null) {
			CallResults.ok().put("EmployeeBean", bean);
		} else {
			CallResults.error().put("message", "登录失败！");
		}
		return null;
	}

	/**
	 * 添加雇员接口
	 * 
	 * @param bean
	 *            雇员信息Bean
	 * @return 新雇员的id
	 */
	@RequestMapping(value = "employeeAdd", produces = "application/json;charset=UTF-8")
	@ApiOperation(notes = "添加雇员信息", value = "添加雇员信息", httpMethod = "POST")
	@ApiImplicitParam(name = "Bean", value = "雇员信息id", required = true, paramType = "body", dataType = "EmployeeBean")
	public CallResults employeeAdd(@RequestBody EmployeeBean bean) {
		Long result = employeeService.employeeAdd(bean);
		if (result > 0) {
			return CallResults.ok().put("EmployeeId", result);
		} else {
			return CallResults.error().put("message", "添加雇员失败！");
		}
	}

	/**
	 * 更新雇员接口
	 * 
	 * @param bean
	 *            雇员信息Bean
	 * @return
	 */
	@RequestMapping(value = "employeeUpdate", produces = "application/json;charset=UTF-8")
	@ApiOperation(notes = "更新雇员信息", value = "更新雇员信息", httpMethod = "POST")
	@ApiImplicitParam(name = "Bean", value = "雇员信息bean", required = true, paramType = "body", dataType = "EmployeeBean")
	public CallResults employeeUpdate(@RequestBody EmployeeBean bean) {
		Integer result = employeeService.employeeUpdate(bean);
		if (result > 0) {
			return CallResults.ok();
		} else {
			return CallResults.error().put("message", "添加雇员失败！");
		}
	}

	/**
	 * 移除雇员信息
	 * 
	 * @param id
	 *            要移除的雇员id
	 * @return
	 */
	@RequestMapping(value = "employeeDelete", produces = "application/json;charset=UTF-8")
	@ApiOperation(notes = "移除雇员信息", value = "移除雇员信息", httpMethod = "POST")
	@ApiImplicitParam(name = "", value = "雇员信息id", required = true, paramType = "body", dataType = "java.lang.Long")
	public CallResults employeeDelete(@RequestBody Long id) {
		Integer result = employeeService.employeeDelete(id);
		if (result > 0) {
			return CallResults.ok();
		} else {
			return CallResults.error().put("message", "添加雇员失败！");
		}
	}

	/**
	 * 获取指定id的雇员信息
	 * 
	 * @param id
	 *            雇员的id
	 * @return 雇员信息Bean
	 */
	@RequestMapping(value = "employeeSelectOneById", produces = "application/json;charset=UTF-8")
	@ApiOperation(notes = "获取雇员信息", value = "获取雇员信息", httpMethod = "GET")
	@ApiImplicitParam(name = "id", value = "雇员信息id", required = true, paramType = "query", dataType = "java.lang.Long")
	public CallResults employeeSelectOneById(@RequestBody Long id) {
		EmployeeBean bean = employeeService.employeeSelectOneById(id);
		if (bean != null) {
			return CallResults.ok().put("EmployeeBean", bean);
		} else {
			return CallResults.error().put("message", "添加雇员失败！");
		}
	}

	/**
	 * 获取指定商户id的雇员集合
	 * 
	 * @param merchantId
	 *            商户的id
	 * @param pageNumber
	 *            第几页
	 * @param pageSize
	 *            页面大小
	 * @return 雇员信息Bean集合
	 */
	@RequestMapping(value = "employeeSelectListByMerchantId", produces = "application/json;charset=UTF-8")
	@ApiOperation(notes = "获取雇员信息集合", value = "获取雇员信息集合", httpMethod = "GET")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "merchantId", value = "商户id", required = true, paramType = "query", dataType = "java.lang.Long"),
			@ApiImplicitParam(name = "pageNumber", value = "第几页", required = true, paramType = "query", dataType = "Integer"),
			@ApiImplicitParam(name = "pageSize", value = "页面大小", required = true, paramType = "query", dataType = "Integer") })
	public CallResults employeeSelectListByMerchantId(@RequestBody Long merchantId, @RequestBody Integer pageNumber,
			@RequestBody Integer pageSize) {
		List<EmployeeBean> bean = employeeService.employeeSelectListByMerchantId(merchantId, pageNumber, pageSize);
		if (bean != null) {
			return CallResults.ok().put("EmployeeBean", bean);
		} else {
			return CallResults.error().put("message", "添加雇员失败！");
		}
	}

	/**
	 * 为雇员关联角色
	 * 
	 * @param bean
	 *            雇员角色关联信息Bean
	 * @return 雇员和角色的关联信息的id
	 */
	@RequestMapping(value = "employeeRoleAdd", produces = "application/json;charset=UTF-8")
	@ApiOperation(notes = "为员工设置角色", value = "为员工设置角色", httpMethod = "POST")
	@ApiImplicitParam(name = "bean", value = "企业员工与角色关系信息Bean", required = true, paramType = "body", dataType = "EmployeeRoleBean")
	public CallResults employeeRoleAdd(@RequestBody EmployeeRoleBean bean) {
		Long result = employeeRoleService.employeeRoleAdd(bean);
		if (result > 0) {
			return CallResults.ok().put("id", result);
		} else {
			return CallResults.error().put("message", "为员工设置角色失败！");
		}
	}

	/**
	 * 移除雇员和角色
	 * 
	 * @param id
	 *            要移除的雇员和角色关联信息id
	 * @return
	 */
	@RequestMapping(value = "employeeRoleDelete", produces = "application/json;charset=UTF-8")
	@ApiOperation(notes = "移除员工的角色", value = "移除员工的角色", httpMethod = "POST")
	@ApiImplicitParam(name = "id", value = "企业员工与角色关系信息id", required = true, paramType = "body", dataType = "java.lang.Long")
	public CallResults employeeRoleDelete(@RequestParam Long id) {
		int result = employeeRoleService.employeeRoleDelete(id);
		if (result == 1) {
			return CallResults.ok();
		} else {
			return CallResults.error().put("message", "受影响的行数：" + result);
		}
	}

	/**
	 * 通过id获取指定的雇员和角色关联信息
	 * 
	 * @param id
	 *            雇员和角色关联信息id
	 * @return 雇员和角色关联信息id
	 */
	@RequestMapping(value = "employeeRoleSelectOneById", produces = "application/json;charset=UTF-8")
	@ApiOperation(notes = "获取员工的角色关系信息", value = "获取员工的角色关系信息", httpMethod = "GET")
	@ApiImplicitParam(name = "id", value = "员工的角色关系信息id", required = true, paramType = "query", dataType = "java.lang.Long")
	public CallResults employeeRoleSelectOneById(@RequestParam Long id) {
		EmployeeRoleBean bean = employeeRoleService.employeeRoleSelectOneById(id);
		return CallResults.ok().put("EmployeeRoleBean", bean);
	}

	/**
	 * 通过雇员id获取该雇员所有的与角色关联信息
	 * 
	 * @param employeeId
	 *            雇员id
	 * @param pageNumber
	 *            第几页
	 * @param pageSize
	 *            页面大小
	 * @return 雇员角色关联信息Bean集合
	 */
	@RequestMapping(value = "employeeRoleSelectListByEmployeeId", produces = "application/json;charset=UTF-8")
	@ApiOperation(notes = "获取员工的角色集合", value = "获取员工的角色集合", httpMethod = "GET")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "employeeId", value = "员工的id", paramType = "query", dataType = "java.lang.Long"),
			@ApiImplicitParam(name = "pageNumber", value = "第几页", paramType = "query", dataType = "Integer"),
			@ApiImplicitParam(name = "pageSize", value = "页面大小", paramType = "query", dataType = "Integer") })
	public CallResults employeeRoleSelectListByEmployeeId(@RequestParam Long employeeId,
			@RequestParam Integer pageNumber, @RequestParam Integer pageSize) {
		List<EmployeeRoleBean> beans = employeeRoleService.employeeRoleSelectListByEmployeeId(employeeId, pageNumber,
				pageSize);
		return CallResults.ok().put("EmployeeRoleBeanList", beans);
	}

}
